package com.open.capacity.reimburse.service.impl;

import com.open.capacity.model.system.LoginAppUser;
import com.open.capacity.reimburse.entity.BillOther2;
import com.open.capacity.utils.SysUserUtil;
import com.open.capacity.workflow.service.WFProcessinstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.open.capacity.commons.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.MapUtils;

import com.open.capacity.reimburse.entity.BillOther1;
import com.open.capacity.reimburse.dao.BillOther1Dao;
import com.open.capacity.reimburse.service.BillOtherService;


@Service
public class BillOtherServiceImpl implements BillOtherService {

    @Autowired
    private BillOther1Dao billDao;

    @Autowired
    private WFProcessinstanceService processinstanceService;

    /**
     * 添加
     *
     * @param bill
     */
    public int save(BillOther1 bill) {

        LoginAppUser loginUser = SysUserUtil.getLoginAppUser();

        bill.setStatus(1);
        bill.setCreateTime(new Date());
        bill.setCreateUserid(loginUser.getId());
        bill.setCreateUsername(loginUser.getRealName());
        int count = billDao.save(bill);

        return count;
    }

    /**
     * 修改
     *
     * @param billOther
     */
    public int update(BillOther1 billOther) {
        return billDao.update(billOther);
    }


    /**
     * 删除
     *
     * @param id
     */
    public int delete(String id) {
        return billDao.delete(id);
    }

    /**
     * 查询记录
     *
     * @param id
     * @return
     */
    public BillOther1 findById(String id) {
        BillOther1 bill =  billDao.findById(id);
        if (bill != null) {
            bill.setProcessCode(processinstanceService.findById(id).getCode());
        }
        return bill;
    }

    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<BillOther1> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        List<BillOther1> list = billDao.findAll(params);
        PageInfo<BillOther1> pageInfo = new PageInfo(list);

        return PageResult.<BillOther1>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<BillOther1> findAll(Map<String, Object> params) {
        return billDao.findAll(params);
    }

}
